package com.mavenq.fly.business.controller;

import com.mavenq.fly.aware.AppEventPublisher;
import com.mavenq.fly.business.entity.Person;
import com.mavenq.fly.commons.resp.ResponseBuilder;
import com.mavenq.fly.commons.resp.ResponseObj;
import com.mavenq.fly.commons.tools.SpringBeanFactory;
import com.mavenq.fly.listener.MsgEvent;
import com.mavenq.fly.listener.PushEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Configuration
@RequestMapping(value = "/events")
public class EventPublishController {

    @Autowired
    private AppEventPublisher appEventPublisher;

    @RequestMapping(value = "publishPushEvent", method = RequestMethod.GET)
    @ResponseBody
    public Object publishPushEvent(){
        Person person = Person.builder().age(30).name("chu").build();
        ApplicationEvent event = new PushEvent<>(person, 1);
        appEventPublisher.publish(event);
        return person;
    }

    @RequestMapping("publish")
    @ResponseBody
    public ResponseObj pushEvent(){
        List<String> names = new ArrayList<>();
        names.add("chupengfei");
        ApplicationContext ctx = SpringBeanFactory.getCtx();
       // ApplicationEvent event = new MsgEvent(names);
        ApplicationEvent event2 = new PushEvent<>(names, 1);
        ctx.publishEvent(event2);
        return ResponseBuilder.buildSuccess("succ");
    }

    @RequestMapping(value = "publishMsgEvent", method = RequestMethod.GET)
    @ResponseBody
    public Object publishMsgEvent(){
        Person person = Person.builder().age(25).name("zhang").build();
        ApplicationEvent event = new MsgEvent(person);
        appEventPublisher.publish(event);
        return person;
    }

}
